import Vue from 'vue'
import VueRouter from 'vue-router'
import List from '../views/List.vue'
import Index from '../views/Index.vue'
import Login from '../views/Login.vue'
import Si from '../views/Si.vue'
import Soc from '../views/Soc.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Index',
    component: Index,
  },
  {
    path: '/list',
    name: 'List',
    component: List,
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/soc',
    name: 'Soc',
    component: Soc,
  },
  {
    path: '*',
    name: 'Si',
    component: Si,
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.name !== 'Login') {
    if (!localStorage.getItem('usr')) {
      next({ name: "Login" })
    } else {
      next();
    }
  } else {
    if (localStorage.getItem('usr')) {
      next({ name: 'Index' })
    } else {
      next()
    }
  }
})

export default router
